{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WfGpInivS0fG"
   },
   "source": [
    "<h2 align=\"center\">点击下列图标在线运行HanLP</h2>\n",
    "<div align=\"center\">\n",
    "\t<a href=\"https://colab.research.google.com/github/hankcs/HanLP/blob/doc-zh/plugins/hanlp_demo/hanlp_demo/zh/amr_restful.ipynb\" target=\"_blank\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\t<a href=\"https://mybinder.org/v2/gh/hankcs/HanLP/doc-zh?filepath=plugins%2Fhanlp_demo%2Fhanlp_demo%2Fzh%2Famr_restful.ipynb\" target=\"_blank\"><img src=\"https://mybinder.org/badge_logo.svg\" alt=\"Open In Binder\"/></a>\n",
    "</div>\n",
    "\n",
    "## 安装"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IYwV-UkNNzFp"
   },
   "source": [
    "无论是Windows、Linux还是macOS，HanLP的安装只需一句话搞定："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "1Uf_u7ddMhUt"
   },
   "outputs": [],
   "source": [
    "!pip install hanlp_restful -U"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "pp-1KqEOOJ4t"
   },
   "source": [
    "## 创建客户端"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "4M7ka0K5OMWU",
    "outputId": "d74f0749-0587-454a-d7c9-7418d45ce534"
   },
   "outputs": [],
   "source": [
    "from hanlp_restful import HanLPClient\n",
    "HanLP = HanLPClient('https://www.hanlp.com/api', auth=None, language='zh') # auth不填则匿名，zh中文，mul多语种"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "BMW528wGNulM"
   },
   "source": [
    "#### 申请秘钥\n",
    "由于服务器算力有限，匿名用户每分钟限2次调用。如果你需要更多调用次数，[建议申请免费公益API秘钥auth](https://bbs.hanlp.com/t/hanlp2-1-restful-api/53)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "elA_UyssOut_"
   },
   "source": [
    "## 抽象意义表示\n",
    "### 中文\n",
    "抽象意义表示任务的输入为一段文本或已分词完毕的句子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "BqEmDMGGOtk3",
    "outputId": "936d439a-e1ff-4308-d2aa-775955558594"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graphs = HanLP.abstract_meaning_representation('男孩希望女孩相信他。')\n",
    "len(graphs)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "jj1Jk-2sPHYx"
   },
   "source": [
    "返回值为每个句子相应的AMR图的Meaning Representation格式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': '0',\n",
       " 'input': '男孩 希望 女孩 相信 他 。',\n",
       " 'nodes': [{'id': 0,\n",
       "   'label': '男孩',\n",
       "   'anchors': [{'from': 0, 'to': 2}, {'from': 12, 'to': 13}]},\n",
       "  {'id': 1, 'label': '希望-01', 'anchors': [{'from': 3, 'to': 5}]},\n",
       "  {'id': 2, 'label': '女孩', 'anchors': [{'from': 6, 'to': 8}]},\n",
       "  {'id': 3, 'label': '相信-01', 'anchors': [{'from': 9, 'to': 11}]}],\n",
       " 'edges': [{'source': 1, 'target': 3, 'label': 'arg1'},\n",
       "  {'source': 1, 'target': 0, 'label': 'arg0'},\n",
       "  {'source': 3, 'target': 2, 'label': 'arg0'},\n",
       "  {'source': 3, 'target': 0, 'label': 'arg1'}],\n",
       " 'tops': [1],\n",
       " 'framework': 'amr'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graph = graphs[0]\n",
    "graph"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "注意上面“男孩”有2个anchor，分别对应“男孩”和“他”。也就是说，MR格式其实包含了指代消解的结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 可视化\n",
    "指定`visualization='svg'`即可得到矢量图可视化。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"171pt\" height=\"298pt\" viewBox=\"0.00 0.00 170.78 297.55\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 293.55)\">\n",
       "<title>0</title>\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-293.55 166.78,-293.55 166.78,4 -4,4\"/>\n",
       "<!-- top -->\n",
       "<!-- 1 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"59.53\" cy=\"-197.46\" rx=\"45.01\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"37.53\" y=\"-193.26\" font-family=\"Times,serif\" font-size=\"14.00\">希望-01</text>\n",
       "</g>\n",
       "<!-- top&#45;&gt;1 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>top-&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M59.53,-253.47C59.53,-245.51 59.53,-235.82 59.53,-226.81\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"63.03,-226.68 59.53,-216.68 56.03,-226.68 63.03,-226.68\"/>\n",
       "</g>\n",
       "<!-- 0 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>0</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"32.53\" cy=\"-19.09\" rx=\"32.55\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"19.53\" y=\"-14.89\" font-family=\"Times,serif\" font-size=\"14.00\">男孩</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;0 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>1-&gt;0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M52.8,-178.56C47.93,-164.78 41.68,-145.14 38.53,-127.37 33.85,-100.97 32.53,-70.4 32.28,-48.67\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"35.78,-48.45 32.23,-38.47 28.78,-48.48 35.78,-48.45\"/>\n",
       "<text text-anchor=\"middle\" x=\"51.03\" y=\"-104.58\" font-family=\"Times,serif\" font-size=\"14.00\">arg0</text>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117.53\" cy=\"-108.28\" rx=\"45.01\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"95.53\" y=\"-104.08\" font-family=\"Times,serif\" font-size=\"14.00\">相信-01</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;3 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>1-&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M71.26,-178.82C79.52,-166.41 90.71,-149.59 100.01,-135.6\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"103.18,-137.16 105.81,-126.89 97.35,-133.28 103.18,-137.16\"/>\n",
       "<text text-anchor=\"middle\" x=\"104.03\" y=\"-149.17\" font-family=\"Times,serif\" font-size=\"14.00\">arg1</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;0 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>3-&gt;0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M101.14,-90.47C88.07,-77.07 69.64,-58.15 55.16,-43.31\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"57.49,-40.69 48.01,-35.97 52.48,-45.57 57.49,-40.69\"/>\n",
       "<text text-anchor=\"middle\" x=\"92.03\" y=\"-59.98\" font-family=\"Times,serif\" font-size=\"14.00\">arg1</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"117.53\" cy=\"-19.09\" rx=\"32.55\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"104.53\" y=\"-14.89\" font-family=\"Times,serif\" font-size=\"14.00\">女孩</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;2 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>3-&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M117.53,-88.79C117.53,-77.03 117.53,-61.59 117.53,-48.32\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"121.03,-48.26 117.53,-38.26 114.03,-48.26 121.03,-48.26\"/>\n",
       "<text text-anchor=\"middle\" x=\"130.03\" y=\"-59.98\" font-family=\"Times,serif\" font-size=\"14.00\">arg0</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from IPython.display import SVG, display\n",
    "\n",
    "def show_svg(g):\n",
    "    display(SVG(data=g['svg']))\n",
    "    \n",
    "graph = HanLP.abstract_meaning_representation('男孩希望女孩相信他。', visualization='svg')[0]\n",
    "show_svg(graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 多语种支持\n",
    "除了中文外，支持的语言列表："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 英文\n",
    "目前，HanLP服务器还支持英文AMR："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"192pt\" height=\"298pt\" viewBox=\"0.00 0.00 191.82 297.55\">\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 293.55)\">\n",
       "<title>0</title>\n",
       "<polygon fill=\"white\" stroke=\"transparent\" points=\"-4,4 -4,-293.55 187.82,-293.55 187.82,4 -4,4\"/>\n",
       "<!-- top -->\n",
       "<!-- 1 -->\n",
       "<g id=\"node2\" class=\"node\">\n",
       "<title>1</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"46.67\" cy=\"-197.46\" rx=\"46.84\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"24.17\" y=\"-193.26\" font-family=\"Times,serif\" font-size=\"14.00\">want-01</text>\n",
       "</g>\n",
       "<!-- top&#45;&gt;1 -->\n",
       "<g id=\"edge1\" class=\"edge\">\n",
       "<title>top-&gt;1</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M46.67,-253.47C46.67,-245.51 46.67,-235.82 46.67,-226.81\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"50.17,-226.68 46.67,-216.68 43.17,-226.68 50.17,-226.68\"/>\n",
       "</g>\n",
       "<!-- 0 -->\n",
       "<g id=\"node3\" class=\"node\">\n",
       "<title>0</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"41.67\" cy=\"-19.09\" rx=\"29.9\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"31.17\" y=\"-14.89\" font-family=\"Times,serif\" font-size=\"14.00\">boy</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;0 -->\n",
       "<g id=\"edge2\" class=\"edge\">\n",
       "<title>1-&gt;0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M44.32,-178.31C42.65,-164.4 40.55,-144.71 39.67,-127.37 38.32,-100.63 39.11,-70.1 40.07,-48.47\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"43.58,-48.48 40.56,-38.33 36.58,-48.15 43.58,-48.48\"/>\n",
       "<text text-anchor=\"middle\" x=\"52.17\" y=\"-104.58\" font-family=\"Times,serif\" font-size=\"14.00\">arg0</text>\n",
       "</g>\n",
       "<!-- 3 -->\n",
       "<g id=\"node4\" class=\"node\">\n",
       "<title>3</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"128.67\" cy=\"-108.28\" rx=\"55.31\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"99.67\" y=\"-104.08\" font-family=\"Times,serif\" font-size=\"14.00\">believe-01</text>\n",
       "</g>\n",
       "<!-- 1&#45;&gt;3 -->\n",
       "<g id=\"edge3\" class=\"edge\">\n",
       "<title>1-&gt;3</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M62.87,-179.23C74.92,-166.42 91.55,-148.74 105.08,-134.36\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"107.92,-136.45 112.22,-126.76 102.82,-131.65 107.92,-136.45\"/>\n",
       "<text text-anchor=\"middle\" x=\"105.17\" y=\"-149.17\" font-family=\"Times,serif\" font-size=\"14.00\">arg1</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;0 -->\n",
       "<g id=\"edge4\" class=\"edge\">\n",
       "<title>3-&gt;0</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M111.48,-90.05C97.99,-76.53 79.08,-57.58 64.35,-42.82\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"66.61,-40.13 57.07,-35.53 61.66,-45.08 66.61,-40.13\"/>\n",
       "<text text-anchor=\"middle\" x=\"103.17\" y=\"-59.98\" font-family=\"Times,serif\" font-size=\"14.00\">arg1</text>\n",
       "</g>\n",
       "<!-- 2 -->\n",
       "<g id=\"node5\" class=\"node\">\n",
       "<title>2</title>\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"128.67\" cy=\"-19.09\" rx=\"28.07\" ry=\"19.18\"/>\n",
       "<text text-anchor=\"start\" x=\"119.17\" y=\"-14.89\" font-family=\"Times,serif\" font-size=\"14.00\">girl</text>\n",
       "</g>\n",
       "<!-- 3&#45;&gt;2 -->\n",
       "<g id=\"edge5\" class=\"edge\">\n",
       "<title>3-&gt;2</title>\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M128.67,-88.79C128.67,-77.03 128.67,-61.59 128.67,-48.32\"/>\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"132.17,-48.26 128.67,-38.26 125.17,-48.26 132.17,-48.26\"/>\n",
       "<text text-anchor=\"middle\" x=\"141.17\" y=\"-59.98\" font-family=\"Times,serif\" font-size=\"14.00\">arg0</text>\n",
       "</g>\n",
       "</g>\n",
       "</svg>"
      ],
      "text/plain": [
       "<IPython.core.display.SVG object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "graph = HanLP.abstract_meaning_representation('The boy wants the girl to believe him.',\n",
    "                                      language='en', visualization='svg')[0]\n",
    "show_svg(graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "用户可以通过指定`language`参数来实现英文抽象意义表示的分析："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': '0',\n",
       " 'input': 'The boy wants the girl to believe him .',\n",
       " 'nodes': [{'id': 0, 'label': 'boy'},\n",
       "  {'id': 1, 'label': 'wants-01'},\n",
       "  {'id': 2, 'label': 'girl'},\n",
       "  {'id': 3, 'label': 'believe-01'}],\n",
       " 'edges': [{'source': 3, 'target': 0, 'label': 'arg1'},\n",
       "  {'source': 1, 'target': 3, 'label': 'arg1'},\n",
       "  {'source': 3, 'target': 2, 'label': 'arg0'},\n",
       "  {'source': 1, 'target': 0, 'label': 'arg0'}],\n",
       " 'tops': [1],\n",
       " 'framework': 'amr'}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "HanLP.abstract_meaning_representation(tokens=[['The', 'boy', 'wants', 'the', 'girl', 'to', 'believe', 'him', '.']], \n",
    "                                            language='en')[0]"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "amr_stl.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
